/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.netflow;

import java.sql.*;
import java.io.*;

import nfse.MetaDatabase;

public class BuildInPlaceNetflow {

    public static void main(String[] args) {
        
        Statement statement = null;
        Statement statement2 = null;
        MetaDatabase mdb = null;

        try {
            String host = args[0];
             mdb = MetaDatabase.create();
            statement = mdb.createStatement();
            statement2 = mdb.createStatement();
            
            statement2.execute("update Data_Info set Min_Time=0 where Sensor_ID=9 and Min_Time=-1");
            
            
            int lastRun = -1;
            String lastDate = "";
            while (true) {
                String sql = "select * from Data_Info where Index_Date > '2006-2-1' and Min_Time=0 and Sensor_ID=9 order by Index_Date desc, Run desc limit 1";
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next()) {
                    int run = rs.getInt("Run");
                    int day = rs.getInt("Day");
                    int year = rs.getInt("Year");
                    int month = rs.getInt("Month");
                    String date = rs.getString("Index_Date");
                    String path = rs.getString("Path");
                    if ((run == lastRun) && (date.equals(lastDate))) {
                        // Last one must have failed, mark this record with a -1
                        statement2.execute("update Data_Info set Min_Time =-1 where Sensor_ID=9 and Run=" + run + " and Index_Date='" + date + "'");
                        System.out.println("Sleeping...");
                        execAndWait("sleep 10");
                    } else {
                        String exec = "perl dataclient_lfap.pl "+host+":9097 " + month + "/" + day + "/"
                                + year + " " + run + " ds";
                        System.out.println(exec);
                        execAndWait(exec);
                        statement2.execute("update Data_Info set Min_Time =1 where Sensor_ID=9 and Run=" + run + " and Index_Date='" + date + "'");
                        
                    }
                    lastRun = run;
                    lastDate = date;
                } else {
                    System.out.println("Sleeping...");
                    execAndWait("sleep 10");

                }
                rs.close();
                
                System.out.println("Sleeping...");
                execAndWait("sleep 10");
                
            }

        } catch (Exception E) {
            E.printStackTrace();
            try {
                statement.close();
                statement2.close();
                mdb.closeConnection();
            } catch (Exception e) {

            }
        }
    }

    private static int execAndWait(String exec) {
        try {
            Process p = Runtime.getRuntime().exec(exec);
            int val = p.waitFor();
            try {
                p.getInputStream().close();
                p.getOutputStream().close();
                p.getErrorStream().close();
                p.destroy();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

            return val;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return -1;
    }

}
